package com.qf.leadnewswemedia.api;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qf.leadnewswemedia.service.WmChannelService;
import com.qf.leadnewswemedia.service.WmNewsService;
import com.qf.leadnewswemedia.service.WmUserService;
import com.qf.qfleadnewsfeignapi.wemedia.WeMediaApi;
import com.qf.qfleadnewsmodel.wemedia.pojos.WmChannel;
import com.qf.qfleadnewsmodel.wemedia.pojos.WmNews;
import com.qf.qfleadnewsmodel.wemedia.pojos.WmUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
public class WeMediaFeignApi implements WeMediaApi {

    @Autowired
    private WmNewsService wmNewsService;

    @Autowired
    private WmUserService wmUserService;

    @Autowired
    private WmChannelService wmChannelService;

    @Override
    public WmNews findWmNewsById(Integer id) {
        return wmNewsService.getById(id);
    }

    @Override
    public String findUsernameById(Integer id) {
        WmUser wmUser = wmUserService.getById(id);
        return wmUser.getName();
    }

    @Override
    public String findChannelnameById(Integer id) {
        WmChannel wmChannel = wmChannelService.getById(id);
        return wmChannel.getName();
    }

    @Override
    public void updateWmnewsStatusById(Integer id, Long articleId) {
        WmNews wmNews = new WmNews();
        wmNews.setId(id);
        wmNews.setStatus((short) 9);
        wmNews.setArticleId(articleId);

        //执行修改
        wmNewsService.updateById(wmNews);
    }

    @Override
    public List<Integer> getChannelIds() {
        Wrapper<WmChannel> qw = Wrappers.lambdaQuery(WmChannel.class)
                .select(WmChannel::getId);

        List<WmChannel> list = wmChannelService.list(qw);

        if (list != null){
            return list.stream().map(channel -> {
                Integer id = channel.getId();
                return id;
            }).collect(Collectors.toList());
        }

        return null;
    }
}
